// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { CupertinoPalette, CupertinoFontSettings } from "styling.slint";
import { FocusBorder } from "components.slint";
import { TabBarBase } from "../common/tabwidget-base.slint";

export component TabWidgetImpl inherits Rectangle {
    in property <length> tabbar-preferred-height;
    in property <length> tabbar-preferred-width;
    in property <length> content-min-height;
    in property <length> content-min-width;
    in property <int> current-index;
    in property <int> current-focused;
    in property <Orientation> orientation: Orientation.horizontal;
    out property <length> content-x: orientation==Orientation.horizontal? 0: root.tabbar-preferred-width;
    out property <length> content-y: orientation==Orientation.horizontal? root.tabbar-preferred-height: 0;
    out property <length> content-height: orientation==Orientation.horizontal? root.height - root.tabbar-preferred-height: root.height;
    out property <length> content-width: orientation==Orientation.horizontal? root.width: root.width - root.tabbar-preferred-width;
    out property <length> tabbar-x: 0;
    out property <length> tabbar-y: 0;
    out property <length> tabbar-height: orientation==Orientation.horizontal? root.tabbar-preferred-height: root.height;
    out property <length> tabbar-width: orientation==Orientation.horizontal? root.width: root.tabbar-preferred-width;

    preferred-width: root.content-min-width;
    min-width: max(root.content-min-width, root.tabbar-preferred-width);
    preferred-height: root.content-min-height + root.tabbar-preferred-height;
    min-height: root.content-min-height + root.tabbar-preferred-height;
}

export component TabImpl inherits Rectangle {
    // The currently focused tab
    in property <int> current-focused;
    // The index of this tab
    in property <int> tab-index;
    // The total number of tabs
    in property <int> num-tabs;
    in property <string> title <=> i-text.text;
    in property <bool> enabled: true;
    out property <bool> has-focus: root.current-focused == root.tab-index;
    // The currently selected tab
    in-out property <int> current;

    private property <bool> hide-right-border: root.tab-index == root.num-tabs - 1 || tab-index + 1 == current;
    private property <bool> is-current: root.tab-index == root.current;

    min-width: max(32px, i-text.min-width);
    min-height: max(20px, i-text.min-height);
    horizontal-stretch: 1;
    vertical-stretch: 0;
    accessible-role: tab;
    accessible-enabled: root.enabled;
    accessible-label: root.title;
    accessible-item-index: root.tab-index;
    accessible-item-selectable: true;
    accessible-item-selected: root.is-current;
    accessible-action-default => { i-touch-area.clicked(); }

    if (root.is-current || i-touch-area.pressed): Rectangle {
        width: 100%;
        height: 100%;
        background: i-touch-area.pressed ? CupertinoPalette.secondary-control-background : CupertinoPalette.control-background;
        border-radius: 5px;
        drop-shadow-blur: 0.25px;
        drop-shadow-color: #0000001f;
        drop-shadow-offset-y: 1px;
        border-width: 1px;
        border-color: CupertinoPalette.decent-border;

        animate background {
            duration: 75ms;
            easing: linear;
        }
    }

    if (!root.is-current && root.tab-index < root.num-tabs - 1): Rectangle {
        x: root.width - self.width;
        y: (parent.height - self.height) / 2;
        width: 1px;
        height: root.height - 6px;
        background: CupertinoPalette.inner-border;
    }

    i-touch-area := TouchArea {
        enabled <=> root.enabled;

        clicked => {
            root.current = root.tab-index;
        }
    }

    i-layout := HorizontalLayout {
        padding-left: 7px;
        padding-right: 7px;

        i-text := Text {
            vertical-alignment: center;
            horizontal-alignment: center;
            font-size: CupertinoFontSettings.body-strong.font-size;
            font-weight: CupertinoFontSettings.body-strong.font-weight;
            color: CupertinoPalette.foreground;
            accessible-role: none;
        }
    }
}

component CupertinoTabBarBase inherits TabBarBase {
    // injected properties:
    // The currently focused tab
    in-out property <int> current-focused: i-focus-scope.has-focus ? i-focus-scope.focused-tab : -1;

    accessible-role: tab-list;
    accessible-delegate-focus: root.current-focused >= 0 ? root.current-focused : root.current;
    accessible-item-count: root.num-tabs;

    Rectangle {
        border-radius: 7px;
        border-color: CupertinoPalette.bar-border;
        background: CupertinoPalette.bar-background;
        border-width: 1px;
        width: 100%;
        height: 100%;

        Rectangle {
            x: (parent.width - self.width) / 2;
            y: parent.border-width;
            width: parent.width - 2 * parent.border-radius;
            height: 1px;
            background: CupertinoPalette.inner-shadow;
        }
    }

    i-focus-scope := FocusScope {
        property <int> focused-tab: 0;

        x: 0;
        // Do not react on clicks
        width: 0px;

        key-pressed(event) => {
            if (event.text == "\n") {
                root.current = root.current-focused;
                return accept;
            }
            if (event.text == Key.LeftArrow) {
                 self.focused-tab = Math.max(self.focused-tab - 1,  0);
                 return accept;
            }
            if (event.text == Key.RightArrow) {
                 self.focused-tab = Math.min(self.focused-tab + 1, root.num-tabs - 1);
                 return accept;
            }
            return reject;
        }

        key-released(event) => {
            if (event.text == " ") {
                root.current = root.current-focused;
                return accept;
            }
            return reject;
        }
    }
}

export component TabBarHorizontalImpl inherits CupertinoTabBarBase {
    Flickable {
        HorizontalLayout {
            min-height: 22px;
            padding: 1px;

            @children
        }
    }
}

export component TabBarVerticalImpl inherits CupertinoTabBarBase {
    Flickable {
        VerticalLayout {
            // TODO min-height: 22px;
            padding: 1px;

            @children
        }
    }
}

export component TabWidget inherits TabWidget { }
